<?php

/**
 * This is the model class for table "quotation_order_header".
 *
 * The followings are the available columns in table 'quotation_order_header':
 * @property string $quotation_no
 * @property string $customer
 * @property string $attention
 * @property string $address
 * @property string $project
 * @property string $reff
 * @property string $date
 * @property string $currency
 * @property integer $discount
 * @property string $employee_id
 *
 * The followings are the available model relations:
 * @property QuotationOrderDetail[] $quotationOrderDetails
 * @property User $employee
 */
class QuotationOrderHeader extends CActiveRecord
{
	public $customer_name;
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'quotation_order_header';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('quotation_no, customer, attention, date, currency, discount, delivery, payment, validity', 'required'),
			array('discount, validity', 'numerical', 'integerOnly'=>true),
			array('customer_name,quotation_no,PO_ref', 'length', 'max'=>50),
			array('customer, attention, reff, remarks', 'length', 'max'=>40),
            array('delivery, payment', 'length', 'max'=>50),
			array('project', 'length', 'max'=>20),
			array('currency', 'length', 'max'=>5),
			array('employee_id', 'length', 'max'=>11),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('quotation_no, customer, attention, address, project, reff, date, currency, discount, employee_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'quotationOrderDetails' => array(self::HAS_MANY, 'QuotationOrderDetail', 'quotation_no'),
			'Employee' => array(self::BELONGS_TO, 'User', 'employee_id'),
			'po_header' => array(self::HAS_MANY, 'PurchaseOrderHeader', 'quotation_no'),
			'Customer' => array(self::BELONGS_TO, 'Customer', 'customer'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'quotation_no' => 'QUOTATION NO',
			'customer' => 'CUSTOMER',
			'Customer.customer' => 'CUSTOMER',
			'attention' => 'ATTENTION',
			'project' => 'PROJECT',
			'reff' => 'REFF',
			'date' => 'DATE',
			'currency' => 'CURRENCY',
			'discount' => 'DISCOUNT',
			'employee_id' => 'EMPLOYEE',
			'Name'=>'CUSTOMER',
            'delivery'=>'DELIVERY',
            'payment'=>'PAYMENT',
            'validity'=>'VALIDITY',
            'PO_ref'=>'PO Reference',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('quotation_no',$this->quotation_no,true);
		$criteria->compare('customer',$this->customer,true);
		$criteria->compare('attention',$this->attention,true);
		$criteria->compare('currency',$this->currency,true);
		$criteria->compare('discount',$this->discount);
		$criteria->compare('employee_id',$this->employee_id,true);
		$criteria->compare('PO_ref',$this->PO_ref,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return QuotationOrderHeader the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	public function beforeSave() {

	    if (parent::beforeSave()) {

	        $this->status_delete='-';
	        $this->employee_id=Yii::app()->session['id'];
	        $this->generate=0;
	        
	        return true;
	    }
	}

	protected function afterFind()
	{
	    parent::afterFind();
	    $this->customer_name = $this->Customer->customer;
	}
}
